// 简单的事件总线，用于组件间通信
class EventBus {
  private events: { [key: string]: Function[] } = {}

  // 监听事件
  on(event: string, callback: Function) {
    if (!this.events[event]) {
      this.events[event] = []
    }
    this.events[event].push(callback)
  }

  // 移除事件监听
  off(event: string, callback: Function) {
    if (!this.events[event]) return
    this.events[event] = this.events[event].filter(cb => cb !== callback)
  }

  // 触发事件
  emit(event: string, ...args: any[]) {
    if (!this.events[event]) return
    this.events[event].forEach(callback => callback(...args))
  }

  // 清除所有事件
  clear() {
    this.events = {}
  }
}

// 导出单例
export const eventBus = new EventBus()

// 预定义的事件类型
export const EVENT_TYPES = {
  USER_LOGIN: 'user:login',
  USER_LOGOUT: 'user:logout',
  USER_INFO_UPDATE: 'user:info:update'
} as const